package com.sz.app.edi.rest.api;

import com.sz.app.edi.rest.api.dto.EdiUserAccountDto;
import com.sz.app.edi.service.constants.EdiCommCodes;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.common.customer.entity.CusUser;
import com.sz.biz.common.customer.service.CustomerService;
import com.sz.biz.common.customer.service.CustomerUserService;
import com.sz.common.base.cache.redis.RedisPoolManager;
import com.sz.common.base.exception.CommonErrorCodes;
import com.sz.common.base.exception.Exceptions;
import com.sz.common.base.utils.SerializeUtil;
import com.sz.common.base.utils.StringUtils;
import com.sz.common.core.system.dto.FileInfoDto;
import com.sz.common.core.system.service.SysFileGroupService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.UUID;

/**
 * Function: EDIController基类. <br>
 * Date: 2017年12月25日 16:36 <br>
 * Author: wentao.chang
 */
class EdiBaseController extends BaseController {

    @Autowired
    CustomerService customerService;
    @Autowired
    CustomerUserService customerUserService;

    @Autowired
    SysFileGroupService fileGroupService;

    static int CUS_ACCOUNT = 1;
    static int SUP_ACCOUNT = 2;

    EdiUserAccountDto getUserInfoByToken(String token) {
        int acoountType = CUS_ACCOUNT;
        Jedis jedis = RedisPoolManager.getJedis();
        byte[] sessionObj = jedis.hget("shiro-activeSessionCache-customer".getBytes(), token.getBytes());
        if (sessionObj == null) {
            sessionObj = jedis.hget("shiro-activeSessionCache-supplier".getBytes(), token.getBytes());
            acoountType = SUP_ACCOUNT;
        }
        jedis.close();
        if (sessionObj == null) {
            throw Exceptions.bizException(CommonErrorCodes.ERROR_401_MSG);
        }
        SimpleSession session = (SimpleSession) SerializeUtil.unserialize(sessionObj);

        EdiUserAccountDto dto = new EdiUserAccountDto();
        if (acoountType == CUS_ACCOUNT) {
            CusUser cusUser = customerUserService.findByAccountName(session.getAttribute("username").toString());
            dto.setCustomerId(cusUser.getRefCustomerId());
            dto.setUserName(cusUser.getFullName());
            dto.setUserCode(cusUser.getUserName());
            dto.setAccountType(acoountType);
            dto.setUserId(cusUser.getId());
            dto.setIsMasterAccount(cusUser.getIsSystem());
            if ( StringUtils.isNotEmpty(cusUser.getUserAvatar())) {
                List<FileInfoDto> attachments = fileGroupService.getFileDtosByGroupId(cusUser.getUserAvatar());
                if (attachments != null && attachments.size() > 0) {
                    dto.setAvatar(attachments.get(0).getPath());
                }
            }
        }
        if (acoountType == SUP_ACCOUNT) {
            //TODO 获取供应商的信息
        }

        return dto;
    }

    void checkLogin(String token) {
        if (!SecurityUtils.getSubject().isAuthenticated() && StringUtils.isNotEmpty(token)) {
            //检查token有效
            getUserInfoByToken(token);
            Subject subject = SecurityUtils.getSubject();
            UsernamePasswordToken loginToken = new UsernamePasswordToken(EdiCommCodes.MOCK_USER_PREFIX +
                    UUID.randomUUID().toString(), "111111");
            //模拟登录
            subject.login(loginToken);
        }
    }
}
